package com.kimo.shop.service.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kimo.shop.bean.dto.FinanceDetailsDto;
import com.kimo.shop.bean.dto.RevenueOverviewDto;
import com.kimo.shop.bean.dto.RevenueOverviewDto.TrendData;
import com.kimo.shop.bean.dto.StationDto;
import com.kimo.shop.bean.model.OrderItem;
import com.kimo.shop.bean.param.FinanceDetailsParam;
import com.kimo.shop.bean.param.RevenueOverviewParam;
import com.kimo.shop.common.util.Arith;
import com.kimo.shop.common.util.DateParam;
import com.kimo.shop.common.util.DateUtils;
import com.kimo.shop.common.util.PageParam;
import com.kimo.shop.dao.RevenueOverviewMapper;
import com.kimo.shop.service.RevenueOverviewService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 财务管理—营收概况
 *
 * @author kimo-taozhouchuan
 */
@Service
@AllArgsConstructor
public class RevenueOverviewServiceImpl extends ServiceImpl<RevenueOverviewMapper, OrderItem> implements RevenueOverviewService {


    private final RevenueOverviewMapper revenueOverviewMapper;

    @Override
    public RevenueOverviewDto getData(RevenueOverviewParam param) throws ParseException {
        Date startDate = new Date(param.getStart());
        Date endDate = new Date(param.getEnd() + (24 * 60 * 60 * 1000) - 1000);

        param.setStartTime(startDate);
        param.setEndTime(endDate);

        // 获取时间段内的实际收入、收入金额、支出金额
        RevenueOverviewDto nowPeriod = revenueOverviewMapper.getData(param.getStationId(), param.getStartTime(), param.getEndTime());
        nowPeriod.setActualIncome(Arith.sub(nowPeriod.getIncomeAmount(), nowPeriod.getPayoutAmount()));

        // 趋势图数据
        List<DateParam> everyDays = DateUtils.findEveryDays(param.getStartTime(), param.getEndTime());
        List<TrendData> trendDataList = new ArrayList<>();
        for (DateParam day : everyDays) {
            RevenueOverviewDto data = revenueOverviewMapper.getData(param.getStationId(), day.getStartTime(), day.getEndTime());
            TrendData trendData = new TrendData();

            trendData.setCurrentDate(DateUtil.format(day.getStartTime(), "yyyy-MM-dd"));
            trendData.setIncomeAmount(data.getIncomeAmount());
            trendData.setPayoutAmount(data.getPayoutAmount());
            trendData.setActualIncome(Arith.sub(data.getIncomeAmount(), data.getPayoutAmount()));

            trendDataList.add(trendData);
        }
        nowPeriod.setTrendDataList(trendDataList);

        // 修改时间
        param.setTime(param.getStartTime(), param.getEndTime());

        // 获取前一时间段内的实际收入、收入金额、支出金额
        RevenueOverviewDto oldPeriod = revenueOverviewMapper.getData(param.getStationId(), param.getStartTime(), param.getEndTime());
        oldPeriod.setActualIncome(Arith.sub(oldPeriod.getIncomeAmount(), oldPeriod.getPayoutAmount()));

        // 实际收入-环比 (本期-上期)/上期*100%
        double actualIncomeRatio = Arith.sub(nowPeriod.getActualIncome(), oldPeriod.getActualIncome());
        actualIncomeRatio = Arith.div(actualIncomeRatio, oldPeriod.getActualIncome(), 2);
        nowPeriod.setActualIncomeRatio(actualIncomeRatio);
        // 收入金额-环比
        double incomeAmountRatio = Arith.sub(nowPeriod.getIncomeAmount(), oldPeriod.getIncomeAmount());
        incomeAmountRatio = Arith.div(incomeAmountRatio, oldPeriod.getIncomeAmount(), 2);
        nowPeriod.setIncomeAmountRatio(incomeAmountRatio);
        // 支出金额-环比
        double payoutAmountRatio = Arith.sub(nowPeriod.getPayoutAmount(), oldPeriod.getPayoutAmount());
        payoutAmountRatio = Arith.div(payoutAmountRatio, oldPeriod.getPayoutAmount(), 2);
        nowPeriod.setPayoutAmountRatio(payoutAmountRatio);


        return nowPeriod;
    }

    @Override
    public IPage<FinanceDetailsDto> getPageDetail(PageParam<FinanceDetailsDto> page, FinanceDetailsParam param) {
        param.setTime();
        param.setEndTime(DateUtil.endOfDay(param.getEndTime()));
        return revenueOverviewMapper.getPageDetail(page, param);
    }

    @Override
    public List<FinanceDetailsDto> getFinanDetailForm(FinanceDetailsParam param) {
        param.setTime();
        return revenueOverviewMapper.getFinanDetailForm(param);
    }

    @Override
    public List<StationDto> getStationList(Integer shopId) {
        return revenueOverviewMapper.getStationList(shopId);
    }

}
